package com.ruoyi.factory.qylock.service.impl;

import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.factory.common.utils.IdGen;
import com.ruoyi.factory.qylock.domain.LaborConFileVO;
import com.ruoyi.factory.qylock.domain.LaborConVO;
import com.ruoyi.factory.qylock.mapper.LaborConFileMapper;
import com.ruoyi.factory.qylock.mapper.LaborConMapper;
import com.ruoyi.factory.qylock.service.QysHandleService;
import com.ruoyi.factory.qylock.service.QysLaborService;
import com.ruoyi.factory.qylock.utils.OkHttpUtils;
import com.ruoyi.factory.qylock.utils.UicodeBackslashU;
import net.sf.json.JSONArray;
import net.sf.json.JSONObject;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.text.SimpleDateFormat;
import java.util.*;

/**
 * @ClassName QysLaborServiceImpl
 * @Description: TODO 人力劳动合同 保密协议
 * @Author LiuJiaji
 * @Date 2023/11/6 15:01
 **/
@Service
public class QysLaborServiceImpl implements QysLaborService {
    protected final Logger logger = LoggerFactory.getLogger(QysLaborServiceImpl.class);

    @Autowired
    private LaborConMapper laborConMapper;
    @Autowired
    private LaborConFileMapper laborConFileMapper;
    @Autowired
    private QysHandleService qysHandleService;


    public AjaxResult upStatus(Map<String,String> request){
        //根据文件id修改状态
        //解析数据
        JSONObject jsonObject = JSONObject.fromObject(request);
        logger.info("人力合同台账契约锁回调参数"+jsonObject.toString());
        String status = jsonObject.optString("status");

        //判断状态
        if("COMPLETE".equals(status)){
            String fileid = jsonObject.optString("contractId");
            String fileid2 = jsonObject.optString("contractId");
            if(StringUtils.isBlank(fileid)) return null;
            LaborConVO laborConVO = new LaborConVO();
            laborConVO.setGlbdef2(fileid);
            List<LaborConVO> laborConVOS = laborConMapper.seleLaborConsta(laborConVO);
            List<LaborConVO> laborConVOS2 = laborConMapper.seleSecreConsta(laborConVO);
            if(!laborConVOS.isEmpty()){
                laborConVO.setGlbdef1("已签署");
                laborConMapper.updateinfo2(laborConVO);
            }else if(!laborConVOS2.isEmpty()){
                laborConVO.setGlbdef1("已签署");
                laborConMapper.updateSecreConInfo2(laborConVO);
            }
        }
        return new AjaxResult(200,"成功");
    }

    /**
     * @author: Liujiaji
     * @date: 2023/11/15 13:54
     * @description: TODO 保密协议自动签署
     * @param: [type]
     * @return: void
     **/
    public void secreContracthand(String type){
        String flowID = "";
        String templateID = "";

        // 1.1查询昨天 签署状态为非签署中和签署完成的人
        String ptype = "secre";
        List<LaborConVO> laborConVOS = findnPerson(ptype);
        // 2.调用契约锁进行签署
        for (LaborConVO laborConVO : laborConVOS) {
            //2.1.1 获取签章流程 及 获取签章流程中模板
            Map<String, String> fandTid = getFandTid(type,laborConVO.getOrgid());
            if(null != fandTid){
                flowID=fandTid.get("flowID");
                templateID=fandTid.get("templateID04");
            }
            //2.1整理参数
            //2.3请求契约锁
            if(StringUtils.isEmpty(flowID) || StringUtils.isEmpty(templateID)) continue;
            AjaxResult pushtoqys = secrepushtoqys(laborConVO, flowID, templateID);
            //2.4契约锁返回结果处理
//            JSONObject jsonObject = JSONObject.fromObject("{\"code\":0,\"contractId\":\"3159776064444891998\",\"message\":\"SUCCESS\"}");
//            AjaxResult pushtoqys = new AjaxResult(200,"请求成功",jsonObject);
            JSONObject jsonObject1 = JSONObject.fromObject(pushtoqys);
            String code = jsonObject1.optString("code");

            if("200".equals(code)){
                JSONObject resj = JSONObject.fromObject(jsonObject1.optJSONObject("data"));
                //3更新状态
                String code1 = resj.optString("code");
                String message = resj.optString("message");
                if("0".equals(code1) && "SUCCESS".equals(message)){
                    //成功 更新状态
                    String contractId = resj.optString("contractId");
                    laborConVO.setGlbdef2(contractId);
                    laborConVO.setGlbdef1("签署中");
                    laborConMapper.updateSecreConInfo(laborConVO);
                    //将附件信息插入附件信息库 根据电子合同id查询是否用
                    LaborConFileVO lcf = new LaborConFileVO();
                    lcf.setDr("0");
                    lcf.setPk_doc(laborConVO.getGlbdef2());
                    //先查询是否已经插入
                    List<LaborConFileVO> selefjfile = laborConFileMapper.selefjfile(lcf);
                    if(selefjfile.isEmpty()){
                        long time = new Date().getTime();
                        SimpleDateFormat spld = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
                        String format = spld.format(time);
                        String pk = time+IdGen.uuid().substring(0,7);
                        lcf.setFilepath(laborConVO.getContraid()+"/保密协议__"+laborConVO.getYf_name()+time+".pdf");
                        lcf.setFiletype("other");
                        lcf.setIsdoc("z");
                        lcf.setIsfolder("n");
                        lcf.setLasttime(String.valueOf(time));
                        lcf.setPk(pk);
                        lcf.setTs(format);
                        laborConFileMapper.insertfjfile(lcf);
                    }
                }
            }
        }
    }

    /**
     * @author: Liujiaji
     * @date: 2023/11/13 09:41
     * @description: TODO 人力资源劳动合同自动签署
     * @param: [type, orgid, termtype]
     * @return: void
     **/
    public void laborContracthand(String type){

        String flowID = "";
        String templateID = "";

        // 1.1查询昨天 签署状态为非签署中和签署完成的人
        String ptype = "labor";
        List<LaborConVO> laborConVOS = findnPerson(ptype);
        // 2.调用契约锁进行签署
        for (LaborConVO laborConVO : laborConVOS) {
            //起初为y的跳过 或者 类型不是1, 2的跳过 1=签订; 2=续签;
            if("Y".equals(laborConVO.getAtfist())) continue;
            if(!"1".equals(laborConVO.getContratype()) && !"2".equals(laborConVO.getContratype())) continue;

            //2.1.1 获取签章流程 及 获取签章流程中模板 签订走签订模板,续签走续签模板,可选,续签可以选择模板类型
            Map<String, String> fandTid = getFandTid(type,laborConVO.getOrgid());
            if(null != fandTid && "1".equals(laborConVO.getContratype())){
                flowID=fandTid.get("flowID");
                templateID=fandTid.get("templateID01");
            }else if(null != fandTid && "2".equals(laborConVO.getContratype())){
                if("标准合同模板".equals(laborConVO.getHtmub())){
                    flowID=fandTid.get("flowID");
                    templateID=fandTid.get("templateID01");
                }else{
                    flowID=fandTid.get("flowID");
                    templateID=fandTid.get("templateID201");
                }
            }
            //2.1整理参数
            //2.3请求契约锁
            if(StringUtils.isEmpty(flowID) || StringUtils.isEmpty(templateID)) continue;
            AjaxResult pushtoqys=new AjaxResult();

            if("1".equals(laborConVO.getContratype())){
                //标准合同模板
                pushtoqys = laborpushtoqys(laborConVO, flowID, templateID);
            }else if("2".equals(laborConVO.getContratype())){
                if("标准合同模板".equals(laborConVO.getHtmub())){
                    pushtoqys = laborpushtoqys(laborConVO, flowID, templateID);
                }else{
                    //续签合同模板
                    pushtoqys = laborpushtoqys2(laborConVO, flowID, templateID);
                }
            }

            //2.4契约锁返回结果处理
//            JSONObject jsonObject = JSONObject.fromObject("{\"code\":0,\"contractId\":\"3159776064444891998\",\"message\":\"SUCCESS\"}");
//            AjaxResult pushtoqys = new AjaxResult(200,"请求成功",jsonObject);
            JSONObject jsonObject1 = JSONObject.fromObject(pushtoqys);
            String code = jsonObject1.optString("code");

            if("200".equals(code)){
                JSONObject resj = JSONObject.fromObject(jsonObject1.optJSONObject("data"));
                //3更新状态
                String code1 = resj.optString("code");
                String message = resj.optString("message");
                if("0".equals(code1) && "SUCCESS".equals(message)){
                    //成功 更新状态
                    String contractId = resj.optString("contractId");
                    laborConVO.setGlbdef2(contractId);
                    laborConVO.setGlbdef1("签署中");
                    laborConMapper.updateinfo(laborConVO);
                    //将附件信息插入附件信息库 根据电子合同id查询是否用
                    LaborConFileVO lcf = new LaborConFileVO();
                    lcf.setDr("0");
                    lcf.setPk_doc(laborConVO.getGlbdef2());
                    //先查询是否已经插入
                    List<LaborConFileVO> selefjfile = laborConFileMapper.selefjfile(lcf);
                    if(selefjfile.isEmpty()){
                        long time = new Date().getTime();
                        SimpleDateFormat spld = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
                        String format = spld.format(time);
                        String pk = time+IdGen.uuid().substring(0,7);
                        lcf.setFilepath(laborConVO.getContraid()+"/劳动合同__"+laborConVO.getYf_name()+time+".pdf");
                        lcf.setFiletype("other");
                        lcf.setIsdoc("z");
                        lcf.setIsfolder("n");
                        lcf.setLasttime(String.valueOf(time));
                        lcf.setPk(pk);
                        lcf.setTs(format);
                        laborConFileMapper.insertfjfile(lcf);
                    }
                }
            }
        }
    }

    /**
     * @author: Liujiaji
     * @date: 2023/11/8 09:03
     * @description: TODO 获取昨天未签署合同的人
     * @param: []
     * @return: java.util.List<com.ruoyi.factory.qylock.domain.LaborConVO>
     **/
    public List<LaborConVO> findnPerson(String type){

        Calendar calendar = Calendar.getInstance();
        calendar.add(Calendar.MONTH,-2);
        Date time = calendar.getTime();
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
        String format = sdf.format(time);
        LaborConVO param = new LaborConVO();
        //业务发生日期
        param.setCreatedate(format);
        //未签署状态
        param.setGlbdef1("");
        if ("labor".equals(type)){
            List<LaborConVO> laborConVOS = laborConMapper.selectLaborCon(param);
            if(!laborConVOS.isEmpty()){
                return laborConVOS;
            }
            return laborConVOS;
        }else{
            List<LaborConVO> laborConVOS = laborConMapper.selectSecreCon(param);
            if(!laborConVOS.isEmpty()){
                return laborConVOS;
            }
            return laborConVOS;
        }


    }

    /**
     * @author: Liujiaji
     * @date: 2023/11/8 09:02
     * @description: TODO 获取签章流程
     * @param: []
     * @return: java.util.Map<java.lang.String,java.lang.String>
     **/
    public Map<String,String> getFandTid(String type, String orgid){
        // 1.1获取签章流程
        JSONObject param2 = new JSONObject();
        param2.put("billtype", type);
        param2.put("pk_org", orgid);
        AjaxResult ajaxResult = qysHandleService.normalTemplateInfo(param2.toString());
        JSONArray resj = (JSONArray)ajaxResult.get("data");
        String flowID = "";
//        String flowID2 = "";
        String templateID01 = "";
        String templateID02 = "";
        String templateID201 = "";
        String templateID04 = "";


        for (Object o : resj) {
            JSONObject res = JSONObject.fromObject(o);
            //流程id
            String name = res.getString("name");
            if(name.contains("1001LD1BZ01")){
                //签订流程
                flowID = res.getString("id");
//            }else if(name.contains("1001LD1BZ02")){
//                //续签流程
//                flowID2 = res.getString("id");
            }
        }
        //1.2 获取签章流程中模板
        if(StringUtils.isNotEmpty(flowID)){
            Map<String, String> stringStringMap = qysHandleService.getqysfileconfig2(flowID);
            //模板id 01劳动合同签订 02模板为保密协议模板
            templateID01 = stringStringMap.get("01");
            templateID02 = stringStringMap.get("02");
            templateID201 = stringStringMap.get("03");
            templateID04 = stringStringMap.get("04");
        }
        //签章劳动合同续签模板
//        if(StringUtils.isNotEmpty(flowID2)){
//            Map<String, String> stringStringMap = qysHandleService.getqysfileconfig2(flowID2);
//            //模板id 02模板为保密协议模板
//            templateID201 = stringStringMap.get("01");
//        }
        //设置返回结果
        Map<String,String> resm = new HashMap<>();
        if(StringUtils.isNotEmpty(flowID) ){
            resm.put("flowID",flowID);
            if(StringUtils.isNotEmpty(templateID01)){
                resm.put("templateID01",templateID01);
            }
            if(StringUtils.isNotEmpty(templateID02)){
                resm.put("templateID02",templateID02);
            }
//            resm.put("flowID2",flowID2);
            if(StringUtils.isNotEmpty(templateID201)){
                resm.put("templateID201",templateID201);
            }
            if(StringUtils.isNotEmpty(templateID04)){
                resm.put("templateID04",templateID04);
            }
            return resm;
        }

        return resm;
    }

    /**
     * @author: Liujiaji
     * @date: 2023/11/7 08:50
     * @description: TODO 整理传入契约锁接口数据
     * @param: [laborConVO]
     * @return: void
     **/
    public AjaxResult laborpushtoqys (LaborConVO laborConVO,String flowID, String templateID){

        //信息参数校验 防止有信息漏填 地址, 民族
        if(StringUtils.isEmpty(laborConVO.getCensusaddr()) || StringUtils.isEmpty(laborConVO.getMz_name())){
            logger.info(laborConVO.getYf_name()+"劳动合同信息缺失"+laborConVO.toString());
            return new AjaxResult(500,"请求失败");
        }
        //处理名字携带数字
        String yf_name = laborConVO.getYf_name();
        if(StringUtils.isNotEmpty(yf_name)){
            laborConVO.setYf_name(yf_name.replaceAll("\\d", ""));
        }

        if(StringUtils.isNotEmpty(flowID) && StringUtils.isNotEmpty(templateID) && StringUtils.isNotEmpty(laborConVO.getOrghname())){
            JSONObject param = new JSONObject();
            param.put("subject","劳动合同--"+laborConVO.getYf_name());
            param.put("categoryId",flowID);
            param.put("tenantName",laborConVO.getOrghname());
            String sn="";
            if(StringUtils.isNotEmpty(laborConVO.getContracode())){
                sn=laborConVO.getContracode();
            }else{
                sn=IdGen.uuid();
            }
            param.put("sn", sn);

            JSONObject signatoriesb1 = new JSONObject();
            signatoriesb1.put("tenantType","PERSONAL");
            signatoriesb1.put("tenantName",laborConVO.getYf_name());
            signatoriesb1.put("serialNo","1");
            signatoriesb1.put("receiverName",laborConVO.getYf_name());
            signatoriesb1.put("contact",laborConVO.getPhonenum());
            JSONObject signatoriesb2 = new JSONObject();
            signatoriesb2.put("tenantType","COMPANY");
            signatoriesb2.put("tenantName",laborConVO.getOrghname());
            signatoriesb2.put("serialNo","2");
//            signatoriesb2.put("receiverName",laborConVO.getPhonenum());
//            signatoriesb2.put("contact",laborConVO.getPhonenum());
            signatoriesb2.put("receiverName","刘家佶");
            signatoriesb2.put("contact","18238687266");
            signatoriesb2.put("remind",false);
            JSONArray signatoriesbl1= new JSONArray();
            signatoriesbl1.add(signatoriesb1);
            signatoriesbl1.add(signatoriesb2);

            param.put("signatories",signatoriesbl1);

            List<String> list = new ArrayList<>();
            list.add(templateID);
            param.put("templates",list);

            JSONArray documentParamsl1= new JSONArray();

            String begindate = laborConVO.getBegindate();
            String enddate = laborConVO.getEnddate();

            String byear = begindate.substring(0, 4);
            String bmon = begindate.substring(5, 7);
            String bday = begindate.substring(8, 10);

            String eyear = enddate.substring(0, 4);
            String emon = enddate.substring(5, 7);
            String eday = enddate.substring(8, 10);
            JSONObject documentParams1 = new JSONObject();
            documentParams1.put("name","签订日期年");
            documentParams1.put("value",byear);
            documentParamsl1.add(documentParams1);

            JSONObject documentParams2 = new JSONObject();
            documentParams2.put("name","签订日期月");
            documentParams2.put("value",bmon);
            documentParamsl1.add(documentParams2);

            JSONObject documentParams3 = new JSONObject();
            documentParams3.put("name","签订日期日");
            documentParams3.put("value",bday);
            documentParamsl1.add(documentParams3);

            JSONObject documentParams4 = new JSONObject();
            documentParams4.put("name","乙方");
            documentParams4.put("value",laborConVO.getYf_name());
            documentParamsl1.add(documentParams4);

            JSONObject documentParams5 = new JSONObject();
            documentParams5.put("name","民族");
            documentParams5.put("value",laborConVO.getMz_name());
            documentParamsl1.add(documentParams5);

            JSONObject documentParams6 = new JSONObject();
            documentParams6.put("name","联系电话");
            documentParams6.put("value",laborConVO.getPhonenum());
            documentParamsl1.add(documentParams6);

            JSONObject documentParams7 = new JSONObject();
            documentParams7.put("name","性别");
            documentParams7.put("value",laborConVO.getSex());
            documentParamsl1.add(documentParams7);

            if("身份证".equals(laborConVO.getZj_name())){
                JSONObject documentParams8 = new JSONObject();
                documentParams8.put("name","身份证号码");
                documentParams8.put("value",laborConVO.getCard_id());
                documentParamsl1.add(documentParams8);
            }

            JSONObject documentParams9 = new JSONObject();
            documentParams9.put("name","联系地址");
            documentParams9.put("value",laborConVO.getCensusaddr());
            documentParamsl1.add(documentParams9);

            if("CM01".equals(laborConVO.getTermtype())){
                JSONObject documentParams10 = new JSONObject();
                documentParams10.put("name","方式种类");
                documentParams10.put("value","1");
                documentParamsl1.add(documentParams10);

                JSONObject documentParams11 = new JSONObject();
                documentParams11.put("name","开始期限年");
                documentParams11.put("value",byear);
                documentParamsl1.add(documentParams11);

                JSONObject documentParams12 = new JSONObject();
                documentParams12.put("name","开始期限月");
                documentParams12.put("value",bmon);
                documentParamsl1.add(documentParams12);

                JSONObject documentParams13 = new JSONObject();
                documentParams13.put("name","开始期限日");
                documentParams13.put("value",bday);
                documentParamsl1.add(documentParams13);

                JSONObject documentParams14 = new JSONObject();
                documentParams14.put("name","结束期限年");
                documentParams14.put("value",eyear);
                documentParamsl1.add(documentParams14);

                JSONObject documentParams15 = new JSONObject();
                documentParams15.put("name","结束期限月");
                documentParams15.put("value",emon);
                documentParamsl1.add(documentParams15);

                JSONObject documentParams16 = new JSONObject();
                documentParams16.put("name","结束期限日");
                documentParams16.put("value",eday);
                documentParamsl1.add(documentParams16);

                String propunit = laborConVO.getPropunit();
                //1=年; 2=月; 3=天;
                if("2".equals(propunit)){
                    String promonth = laborConVO.getPromonth();
                    if(StringUtils.isNotEmpty(promonth)){
                        JSONObject documentParams17 = new JSONObject();
                        documentParams17.put("name","试用期月");
                        documentParams17.put("value",promonth+"个");
                        documentParamsl1.add(documentParams17);
                    }
                }

            }else if ("CM02".equals(laborConVO.getTermtype())){
                JSONObject documentParams10 = new JSONObject();
                documentParams10.put("name","方式种类");
                documentParams10.put("value","2");
                documentParamsl1.add(documentParams10);

                JSONObject documentParams11 = new JSONObject();
                documentParams11.put("name","开始期限年2");
                documentParams11.put("value","1");
                documentParamsl1.add(documentParams11);

                JSONObject documentParams12 = new JSONObject();
                documentParams12.put("name","开始期限月2");
                documentParams12.put("value","1");
                documentParamsl1.add(documentParams12);

                JSONObject documentParams13 = new JSONObject();
                documentParams13.put("name","开始期限日2");
                documentParams13.put("value","1");
                documentParamsl1.add(documentParams13);

            }else if ("CM03".equals(laborConVO.getTermtype())){
                JSONObject documentParams10 = new JSONObject();
                documentParams10.put("name","方式种类");
                documentParams10.put("value","3");
                documentParamsl1.add(documentParams10);

                JSONObject documentParams14 = new JSONObject();
                documentParams14.put("name","结束期限年3");
                documentParams14.put("value","1");
                documentParamsl1.add(documentParams14);

                JSONObject documentParams15 = new JSONObject();
                documentParams15.put("name","结束期限月3");
                documentParams15.put("value","1");
                documentParamsl1.add(documentParams15);

                JSONObject documentParams16 = new JSONObject();
                documentParams16.put("name","结束期限日3");
                documentParams16.put("value","1");
                documentParamsl1.add(documentParams16);

            }
            JSONObject documentParams17 = new JSONObject();
            documentParams17.put("name","工作类别");
            documentParams17.put("value",laborConVO.getWork_class());
            documentParamsl1.add(documentParams17);

            JSONObject documentParams18 = new JSONObject();
            documentParams18.put("name","工作地点");
            documentParams18.put("value",laborConVO.getSignaddr());
            documentParamsl1.add(documentParams18);

            JSONObject documentParams19 = new JSONObject();
            documentParams19.put("name","工作时间");
            documentParams19.put("value",laborConVO.getWork_rule());
            documentParamsl1.add(documentParams19);

            JSONObject documentParams20 = new JSONObject();
            documentParams20.put("name","薪资等级");
            if("1".equals(laborConVO.getContratype())){
                documentParams20.put("value",laborConVO.getWaprmlv());
            }else if("2".equals(laborConVO.getContratype())){
                documentParams20.put("value",laborConVO.getWaprmlvxq());
            }

            documentParamsl1.add(documentParams20);

            param.put("documentParams",documentParamsl1);

            //请求契约锁
            //封装数据
            String url = "/contract/createbycategory";
            //请求契约锁
            logger.info("请求契约锁发起电子签约url: " + url);
            logger.info("人事合同请求契约锁参数 : "+param.toString());
            String s2 = new Date().toString();
            logger.info("请求契约锁发起电子签约时间 : "+s2);
            String res2 = OkHttpUtils.okHttpPostqys(url, param.toString());
            String s3 = new Date().toString();
            logger.info("请求契约锁发起签章返回结果时间 : "+s3);
            res2 = UicodeBackslashU.unicodeToCn(res2);
            logger.info("人事合同请求契约锁发起电子签约返参: " + res2);
            //处理返回结果
            JSONObject res = JSONObject.fromObject(res2);
            return new AjaxResult(200,"请求成功",res);
        }
        return new AjaxResult(500,"请求失败");

    }

    /**
     * @author: Liujiaji
     * @date: 2023/11/7 08:50
     * @description: TODO 整理传入契约锁接口数据
     * @param: [laborConVO]
     * @return: void
     **/
    public AjaxResult laborpushtoqys2 (LaborConVO laborConVO,String flowID, String templateID){

        //信息参数校验 防止有信息漏填 地址, 民族
//        if(StringUtils.isEmpty(laborConVO.getCensusaddr()) || StringUtils.isEmpty(laborConVO.getMz_name())){
//            logger.info(laborConVO.getYf_name()+"劳动合同信息缺失");
//            return new AjaxResult(500,"请求失败");
//        }
        //信息参数校验 防止上次日期为空
        if(StringUtils.isEmpty(laborConVO.getLasttime())){
            logger.info(laborConVO.getYf_name()+"续签劳动合同信息缺失"+laborConVO.toString());
            return new AjaxResult(500,"请求失败");
        }
        //处理名字携带数字
        String yf_name = laborConVO.getYf_name();
        if(StringUtils.isNotEmpty(yf_name)){
            laborConVO.setYf_name(yf_name.replaceAll("\\d", ""));
        }

        if(StringUtils.isNotEmpty(flowID) && StringUtils.isNotEmpty(templateID) && StringUtils.isNotEmpty(laborConVO.getOrghname())){
            JSONObject param = new JSONObject();
            param.put("subject","劳动合同--"+laborConVO.getYf_name());
            param.put("categoryId",flowID);
            param.put("tenantName",laborConVO.getOrghname());
            String sn="";
            if(StringUtils.isNotEmpty(laborConVO.getContracode())){
                sn=laborConVO.getContracode();
            }else{
                sn=IdGen.uuid();
            }
            param.put("sn", sn);

            JSONObject signatoriesb1 = new JSONObject();
            signatoriesb1.put("tenantType","PERSONAL");
            signatoriesb1.put("tenantName",laborConVO.getYf_name());
            signatoriesb1.put("serialNo","1");
            signatoriesb1.put("receiverName",laborConVO.getYf_name());
            signatoriesb1.put("contact",laborConVO.getPhonenum());
            JSONObject signatoriesb2 = new JSONObject();
            signatoriesb2.put("tenantType","COMPANY");
            signatoriesb2.put("tenantName",laborConVO.getOrghname());
            signatoriesb2.put("serialNo","2");
            signatoriesb2.put("receiverName","刘家佶");
            signatoriesb2.put("contact","18238687266");
            signatoriesb2.put("remind",false);
            JSONArray signatoriesbl1= new JSONArray();
            signatoriesbl1.add(signatoriesb1);
            signatoriesbl1.add(signatoriesb2);

            param.put("signatories",signatoriesbl1);

            List<String> list = new ArrayList<>();
            list.add(templateID);
            param.put("templates",list);

            JSONArray documentParamsl1= new JSONArray();

            String begindate = laborConVO.getBegindate();
            String enddate = laborConVO.getEnddate();

            String byear = begindate.substring(0, 4);
            String bmon = begindate.substring(5, 7);
            String bday = begindate.substring(8, 10);

            String eyear = enddate.substring(0, 4);
            String emon = enddate.substring(5, 7);
            String eday = enddate.substring(8, 10);

            String lasttime = laborConVO.getLasttime();
            String lyear = lasttime.substring(0, 4);
            String lmon = lasttime.substring(5, 7);
            String lday = lasttime.substring(8, 10);

            JSONObject documentParams1 = new JSONObject();
            documentParams1.put("name","签订日期年");
            documentParams1.put("value",byear);
            documentParamsl1.add(documentParams1);

            JSONObject documentParams2 = new JSONObject();
            documentParams2.put("name","签订日期月");
            documentParams2.put("value",bmon);
            documentParamsl1.add(documentParams2);

            JSONObject documentParams3 = new JSONObject();
            documentParams3.put("name","签订日期日");
            documentParams3.put("value",bday);
            documentParamsl1.add(documentParams3);

            JSONObject documentParams4 = new JSONObject();
            documentParams4.put("name","乙方");
            documentParams4.put("value",laborConVO.getYf_name());
            documentParamsl1.add(documentParams4);

            JSONObject documentParams5 = new JSONObject();
            documentParams5.put("name","民族");
            documentParams5.put("value",laborConVO.getMz_name());
            documentParamsl1.add(documentParams5);

            JSONObject documentParams6 = new JSONObject();
            documentParams6.put("name","联系电话");
            documentParams6.put("value",laborConVO.getPhonenum());
            documentParamsl1.add(documentParams6);

            JSONObject documentParams7 = new JSONObject();
            documentParams7.put("name","性别");
            documentParams7.put("value",laborConVO.getSex());
            documentParamsl1.add(documentParams7);

            if("身份证".equals(laborConVO.getZj_name())){
                JSONObject documentParams8 = new JSONObject();
                documentParams8.put("name","身份证号码");
                documentParams8.put("value",laborConVO.getCard_id());
                documentParamsl1.add(documentParams8);
            }

            JSONObject documentParams9 = new JSONObject();
            documentParams9.put("name","联系地址");
            documentParams9.put("value",laborConVO.getCensusaddr());
            documentParamsl1.add(documentParams9);


            if("CM01".equals(laborConVO.getTermtype())){
                JSONObject documentParams10 = new JSONObject();
                documentParams10.put("name","方式种类");
                documentParams10.put("value","1");
                documentParamsl1.add(documentParams10);

                JSONObject documentParams11 = new JSONObject();
                documentParams11.put("name","开始期限年");
                documentParams11.put("value",byear);
                documentParamsl1.add(documentParams11);

                JSONObject documentParams12 = new JSONObject();
                documentParams12.put("name","开始期限月");
                documentParams12.put("value",bmon);
                documentParamsl1.add(documentParams12);

                JSONObject documentParams13 = new JSONObject();
                documentParams13.put("name","开始期限日");
                documentParams13.put("value",bday);
                documentParamsl1.add(documentParams13);

                JSONObject documentParams14 = new JSONObject();
                documentParams14.put("name","结束期限年");
                documentParams14.put("value",eyear);
                documentParamsl1.add(documentParams14);

                JSONObject documentParams15 = new JSONObject();
                documentParams15.put("name","结束期限月");
                documentParams15.put("value",emon);
                documentParamsl1.add(documentParams15);

                JSONObject documentParams16 = new JSONObject();
                documentParams16.put("name","结束期限日");
                documentParams16.put("value",eday);
                documentParamsl1.add(documentParams16);

                String propunit = laborConVO.getPropunit();
                //1=年; 2=月; 3=天;
                if("2".equals(propunit)){
                    String promonth = laborConVO.getPromonth();
                    if(StringUtils.isNotEmpty(promonth)){
                        JSONObject documentParams17 = new JSONObject();
                        documentParams17.put("name","试用期月");
                        documentParams17.put("value",promonth+"个");
                        documentParamsl1.add(documentParams17);
                    }
                }

            }else if ("CM02".equals(laborConVO.getTermtype())){
                JSONObject documentParams10 = new JSONObject();
                documentParams10.put("name","方式种类");
                documentParams10.put("value","2");
                documentParamsl1.add(documentParams10);

                JSONObject documentParams11 = new JSONObject();
                documentParams11.put("name","开始期限年2");
                documentParams11.put("value", byear);
                documentParamsl1.add(documentParams11);

                JSONObject documentParams12 = new JSONObject();
                documentParams12.put("name","开始期限月2");
                documentParams12.put("value", bmon);
                documentParamsl1.add(documentParams12);

                JSONObject documentParams13 = new JSONObject();
                documentParams13.put("name","开始期限日2");
                documentParams13.put("value", bday);
                documentParamsl1.add(documentParams13);

            }else if ("CM03".equals(laborConVO.getTermtype())){
                JSONObject documentParams10 = new JSONObject();
                documentParams10.put("name","方式种类");
                documentParams10.put("value","3");
                documentParamsl1.add(documentParams10);

                JSONObject documentParams14 = new JSONObject();
                documentParams14.put("name","结束期限年3");
                documentParams14.put("value","1");
                documentParamsl1.add(documentParams14);

                JSONObject documentParams15 = new JSONObject();
                documentParams15.put("name","结束期限月3");
                documentParams15.put("value","1");
                documentParamsl1.add(documentParams15);

                JSONObject documentParams16 = new JSONObject();
                documentParams16.put("name","结束期限日3");
                documentParams16.put("value","1");
                documentParamsl1.add(documentParams16);

            }
            JSONObject documentParams17 = new JSONObject();
            documentParams17.put("name","工作类别");
            documentParams17.put("value",laborConVO.getWork_class());
            documentParamsl1.add(documentParams17);

            JSONObject documentParams18 = new JSONObject();
            documentParams18.put("name","工作地点");
            documentParams18.put("value",laborConVO.getSignaddr());
            documentParamsl1.add(documentParams18);

            JSONObject documentParams19 = new JSONObject();
            documentParams19.put("name","工作时间");
            documentParams19.put("value",laborConVO.getWork_rule());
            documentParamsl1.add(documentParams19);

            JSONObject documentParams20 = new JSONObject();
            documentParams20.put("name","薪资等级");
            if("1".equals(laborConVO.getContratype())){
                documentParams20.put("value",laborConVO.getWaprmlv());
            }else if("2".equals(laborConVO.getContratype())){
                documentParams20.put("value",laborConVO.getWaprmlvxq());
            }
            documentParamsl1.add(documentParams20);

            JSONObject documentParams21 = new JSONObject();
            documentParams21.put("name","上次签订日期年");
            documentParams21.put("value", lyear);
            documentParamsl1.add(documentParams21);

            JSONObject documentParams22 = new JSONObject();
            documentParams22.put("name","上次签订日期月");
            documentParams22.put("value", lmon);
            documentParamsl1.add(documentParams22);

            JSONObject documentParams23 = new JSONObject();
            documentParams23.put("name","上次签订日期日");
            documentParams23.put("value", lday);
            documentParamsl1.add(documentParams23);

            param.put("documentParams",documentParamsl1);

            //请求契约锁
            //封装数据
            String url = "/contract/createbycategory";
            //请求契约锁
            logger.info("请求契约锁发起电子签约url: " + url);
            logger.info("人事合同请求契约锁参数 : "+param.toString());
            String s2 = new Date().toString();
            logger.info("请求契约锁发起电子签约时间 : "+s2);
            String res2 = OkHttpUtils.okHttpPostqys(url, param.toString());
            String s3 = new Date().toString();
            logger.info("请求契约锁发起签章返回结果时间 : "+s3);
            res2 = UicodeBackslashU.unicodeToCn(res2);
            logger.info("人事合同请求契约锁发起电子签约返参: " + res2);
            //处理返回结果
            JSONObject res = JSONObject.fromObject(res2);
            return new AjaxResult(200,"请求成功",res);
        }
        return new AjaxResult(500,"请求失败");

    }

    /**
     * @author: Liujiaji
     * @date: 2023/11/7 08:50
     * @description: TODO 整理传入契约锁接口数据
     * @param: [laborConVO]
     * @return: void
     **/
    public AjaxResult secrepushtoqys (LaborConVO laborConVO,String flowID, String templateID){

        //处理名字携带数字
        String yf_name = laborConVO.getYf_name();
        if(StringUtils.isNotEmpty(yf_name)){
            laborConVO.setYf_name(yf_name.replaceAll("\\d", ""));
        }
        if(StringUtils.isNotEmpty(flowID) && StringUtils.isNotEmpty(templateID) && StringUtils.isNotEmpty(laborConVO.getOrghname())){
            JSONObject param = new JSONObject();
            param.put("subject","保密协议--"+laborConVO.getYf_name());
            param.put("categoryId",flowID);
            param.put("tenantName",laborConVO.getOrghname());
            String sn="";
            if(StringUtils.isNotEmpty(laborConVO.getContracode())){
                sn=laborConVO.getContracode();
            }else{
                sn=IdGen.uuid();
            }
            param.put("sn", sn);

            JSONObject signatoriesb1 = new JSONObject();
            signatoriesb1.put("tenantType","PERSONAL");
            signatoriesb1.put("tenantName",laborConVO.getYf_name());
            signatoriesb1.put("serialNo","1");
            signatoriesb1.put("receiverName",laborConVO.getYf_name());
            signatoriesb1.put("contact",laborConVO.getPhonenum());
            JSONObject signatoriesb2 = new JSONObject();
            signatoriesb2.put("tenantType","COMPANY");
            signatoriesb2.put("tenantName",laborConVO.getOrghname());
            signatoriesb2.put("serialNo","2");
//            signatoriesb2.put("receiverName",laborConVO.getPhonenum());
//            signatoriesb2.put("contact",laborConVO.getPhonenum());
            signatoriesb2.put("receiverName","刘家佶");
            signatoriesb2.put("contact","18238687266");
            signatoriesb2.put("remind",false);
            JSONArray signatoriesbl1= new JSONArray();
            signatoriesbl1.add(signatoriesb1);
            signatoriesbl1.add(signatoriesb2);

            param.put("signatories",signatoriesbl1);

            List<String> list = new ArrayList<>();
            list.add(templateID);
            param.put("templates",list);

            JSONArray documentParamsl1= new JSONArray();

            String begindate = laborConVO.getBegindate();
//            String enddate = laborConVO.getEnddate();

            String byear = begindate.substring(0, 4);
            String bmon = begindate.substring(5, 7);
            String bday = begindate.substring(8, 10);

//            String eyear = enddate.substring(0, 4);
//            String emon = enddate.substring(5, 7);
//            String eday = enddate.substring(8, 10);
            JSONObject documentParams1 = new JSONObject();
            documentParams1.put("name","签订日期年");
            documentParams1.put("value",byear);
            documentParamsl1.add(documentParams1);

            JSONObject documentParams2 = new JSONObject();
            documentParams2.put("name","签订日期月");
            documentParams2.put("value",bmon);
            documentParamsl1.add(documentParams2);

            JSONObject documentParams3 = new JSONObject();
            documentParams3.put("name","签订日期日");
            documentParams3.put("value",bday);
            documentParamsl1.add(documentParams3);


            JSONObject documentParams6 = new JSONObject();
            documentParams6.put("name","联系电话");
            documentParams6.put("value",laborConVO.getPhonenum());
            documentParamsl1.add(documentParams6);


            if("身份证".equals(laborConVO.getZj_name())){
                JSONObject documentParams8 = new JSONObject();
                documentParams8.put("name","身份证号");
                documentParams8.put("value",laborConVO.getCard_id());
                documentParamsl1.add(documentParams8);
            }

            JSONObject documentParams9 = new JSONObject();
            documentParams9.put("name","住址");
            documentParams9.put("value",laborConVO.getCensusaddr());
            documentParamsl1.add(documentParams9);

            param.put("documentParams",documentParamsl1);

            //请求契约锁
            //封装数据
            String url = "/contract/createbycategory";
            //请求契约锁
            logger.info("请求契约锁发起电子签约url: " + url);
            logger.info("请求契约锁参数 : "+param.toString());
            String s2 = new Date().toString();
            logger.info("请求契约锁发起电子签约时间 : "+s2);
            String res2 = OkHttpUtils.okHttpPostqys(url, param.toString());
            String s3 = new Date().toString();
            logger.info("请求契约锁发起签章返回结果时间 : "+s3);
            res2 = UicodeBackslashU.unicodeToCn(res2);
            logger.info("请求契约锁发起电子签约返参: " + res2);
            //处理返回结果
            JSONObject res = JSONObject.fromObject(res2);
            return new AjaxResult(200,"请求成功",res);
        }
        return new AjaxResult(500,"请求失败");

    }

    public static void main(String[] args) {
        JSONObject param = new JSONObject();
        param.put("subject","人力合同模板");
        param.put("categoryId","用印流程id 321");
        param.put("tenantName","新乡市瑞丰新材料股份有限公司");
        param.put("sn","自定义编码202310201035");

        JSONObject signatoriesb1 = new JSONObject();
        signatoriesb1.put("tenantType","PERSONAL");
        signatoriesb1.put("tenantName","刘家佶");
        signatoriesb1.put("serialNo","1");
        signatoriesb1.put("receiverName","刘家佶");
        signatoriesb1.put("contact","18238687266");
        JSONObject signatoriesb2 = new JSONObject();
        signatoriesb2.put("tenantType","COMPANY");
        signatoriesb2.put("tenantName","新乡市瑞丰新材料股份有限公司");
        signatoriesb2.put("serialNo","2");
        signatoriesb2.put("receiverName","13643909714");
        signatoriesb2.put("contact","13643909714");
        JSONArray signatoriesbl1= new JSONArray();
        signatoriesbl1.add(signatoriesb1);
        signatoriesbl1.add(signatoriesb2);

        param.put("signatories",signatoriesbl1);

        List<String> list = new ArrayList<>();
        list.add("模板id 123456");
        param.put("templates",list);

        JSONObject documentParams1 = new JSONObject();
        documentParams1.put("name","乙方");
        documentParams1.put("value","测试");

        JSONObject documentParams2 = new JSONObject();
        documentParams2.put("name","签订日期日");
        documentParams2.put("value","20");

        JSONObject documentParams3 = new JSONObject();
        documentParams3.put("name","签订日期月");
        documentParams3.put("value","10");

        JSONArray documentParamsl1= new JSONArray();
        documentParamsl1.add(documentParams1);
        documentParamsl1.add(documentParams2);
        documentParamsl1.add(documentParams3);

        param.put("documentParams",documentParamsl1);

        System.out.println("格式处理之后"+param.toString());
    }

}
